package com.sci.web.controller.interceptor;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.sci.config.MyConstants;
import com.sci.web.model.genmodel.User;

public class TokenInterceptor implements Interceptor {

	public void intercept(Invocation ai) {
		
		Controller controller = ai.getController();
		HttpServletRequest request = controller.getRequest();
		String requestURI = request.getRequestURI();
		MyConstants.log.info("====>进入拦截器,请求地址:"+requestURI);
		User user = (User)request.getSession().getAttribute("user");
		
		
		
		
		String whiteAddr = "tologin,login,/";
		
		
		String[] split = whiteAddr.split("\\,");
		
		boolean flag = true;
		for (String string : split) {
			if(requestURI.indexOf(string)>=0){
				flag = false; 
			}
		}
		if(flag){
			if(user!=null){
				ai.invoke();
			}else{
				controller.redirect("/v2/tologin");
				//ai.invoke();
			}
		}else{
			ai.invoke();
		}
		MyConstants.log.info("====>通过拦截器");

	}

}
